let cart = JSON.parse(localStorage.getItem('cart')) || [];


        function addToCart(productId) {
            const product = document.querySelector(`[data-id="${productId}"]`);
            const productName = product.querySelector('h3').textContent;
            const price = parseFloat(product.querySelector('p').textContent.replace('¥', ''));

            const existingItem = cart.find(item => item.id === productId);
            if (existingItem) {
                existingItem.quantity++;
            } else {
                cart.push({
                    id: productId,
                    name: productName,
                    price: price,
                    quantity: 1
                });
            }
            
            updateCartDisplay();
            saveCart();
        }
document.getElementById('cart-toggle').addEventListener('click', function() {
    document.querySelector('.cart').classList.toggle('active');
});
let currentSlide = 0;
let autoSlideInterval;
const slides = document.querySelectorAll('.carousel-item');
const dotsContainer = document.querySelector('.carousel-dots');

// 初始化导航点
slides.forEach((_, index) => {
    const dot = document.createElement('span');
    dot.addEventListener('click', () => goToSlide(index));
    dotsContainer.appendChild(dot);
});
updateDots();

// 自动轮播
function startAutoSlide() {
    autoSlideInterval = setInterval(() => {
        currentSlide = (currentSlide + 1) % slides.length;
        updateSlide();
    }, 5000);
}
startAutoSlide();

// 切换幻灯片
function updateSlide() {
    document.querySelector('.carousel-slide').style.transform = 
        `translateX(-${currentSlide * 100}%)`;
    slides.forEach(slide => slide.classList.remove('active'));
    slides[currentSlide].classList.add('active');
    updateDots();
}

// 更新导航点
function updateDots() {
    document.querySelectorAll('.carousel-dots span').forEach((dot, index) => {
        dot.classList.toggle('active', index === currentSlide);
    });
}
// 在index.html的script中添加

// 手动切换
document.querySelector('.carousel-prev').addEventListener('click', () => {
    currentSlide = (currentSlide - 1 + slides.length) % slides.length;
    updateSlide();
    resetInterval();
});

document.querySelector('.carousel-next').addEventListener('click', () => {
    currentSlide = (currentSlide + 1) % slides.length;
    updateSlide();
    resetInterval();
});

// 跳转到指定幻灯片
function goToSlide(index) {
    currentSlide = index;
    updateSlide();
    resetInterval();
}

// 重置自动轮播间隔
function resetInterval() {
    clearInterval(autoSlideInterval);
    startAutoSlide();
}

let comments = JSON.parse(localStorage.getItem('comments')) || [];

function postComment() {
    const author = document.getElementById('comment-author').value.trim();
    const content = document.getElementById('comment-content').value.trim();
    
    if (!author || !content) {
        alert('请填写昵称和评论内容');
        return;
    }

    const newComment = {
        id: Date.now(),
        author: author,
        content: content,
        likes: 0,
        timestamp: new Date().toISOString()
    };

    comments.unshift(newComment);
    localStorage.setItem('comments', JSON.stringify(comments));
    renderComments();
    
    // 清空输入框
    document.getElementById('comment-content').value = '';
}

function renderComments() {
    const list = document.getElementById('comment-list');
    const count = document.getElementById('comment-count');
    
    list.innerHTML = comments.map(comment => `
        <div class="comment-item">
            <div class="user-avatar">${comment.author[0]}</div>
            <div class="comment-content">
                <div class="comment-header">
                    <strong>${comment.author}</strong>
                    <span class="comment-time">${formatTime(comment.timestamp)}</span>
                </div>
                <p>${comment.content}</p>
                <div class="comment-actions">
                    <button onclick="likeComment(${comment.id})">
                        👍 有用 (${comment.likes})
                    </button>
                </div>
            </div>
        </div>
    `).join('');

    count.textContent = comments.length;
}

function likeComment(commentId) {
    comments = comments.map(comment => {
        if (comment.id === commentId) {
            return { ...comment, likes: comment.likes + 1 };
        }
        return comment;
    });
    localStorage.setItem('comments', JSON.stringify(comments));
    renderComments();
}

function formatTime(isoString) {
    const now = new Date();
    const commentDate = new Date(isoString);
    const diff = Math.floor((now - commentDate) / 1000);
    
    if (diff < 60) return '刚刚';
    if (diff < 3600) return `${Math.floor(diff/60)}分钟前`;
    if (diff < 86400) return `${Math.floor(diff/3600)}小时前`;
    return commentDate.toLocaleDateString();
}

// 初始化渲染
document.addEventListener('DOMContentLoaded', renderComments);
// 悬停暂停
document.querySelector('.carousel-container').addEventListener('mouseenter', () => {
    clearInterval(autoSlideInterval);
});
document.querySelector('.carousel-container').addEventListener('mouseleave', startAutoSlide); 
        function updateCartDisplay() {
            const cartItems = document.getElementById('cart-items');
            const cartCount = document.getElementById('cart-count');
            const totalPrice = document.getElementById('total-price');

            cartItems.innerHTML = '';
            let total = 0;

            cart.forEach(item => {
                total += item.price * item.quantity;
                const div = document.createElement('div');
                div.innerHTML = `
                    ${item.name} × ${item.quantity} 
                    <button onclick="removeItem(${item.id})">删除</button>
                `;
                cartItems.appendChild(div);
            });

            cartCount.textContent = cart.reduce((sum, item) => sum + item.quantity, 0);
            totalPrice.textContent = total.toFixed(2);
			
			document.getElementById('cart-badge').textContent = cart.reduce((sum, item) => sum + item.quantity, 0);
        }

        function removeItem(productId) {
            cart = cart.filter(item => item.id !== productId);
            updateCartDisplay();
            saveCart();
        }

        function clearCart() {
            cart = [];
            updateCartDisplay();
            saveCart();
        }

        function saveCart() {
            localStorage.setItem('cart', JSON.stringify(cart));
        }
function saveCart() {
    if(cart.length === 0) {
        event.preventDefault();
        alert('购物车为空，请先添加商品');
       return;
    }
    
    // 存储购物车数据和总价
    localStorage.setItem('checkoutCart', JSON.stringify({
        items: cart,
        total: cart.reduce((sum, item) => sum + (item.price * item.quantity), 0)
    }));
}

        // 初始化时更新购物车显示
        updateCartDisplay();